package com.tensquare.gathering.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.tensquare.gathering.domain.Gathering;
import com.tensquare.gathering.service.IGatheringService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author hbxfwf
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/gathering")
//@CrossOrigin
public class GatheringController extends BaseController
{
    @Autowired
    private IGatheringService gatheringService;
    @Value("${server.port}")
    private int port;
    /**
     * 查询活动列表
     */
    @PreAuthorize(hasPermi = "gathering:gathering:list")
    @GetMapping("/list")
    public TableDataInfo list(Gathering gathering)
    {
        startPage();
        List<Gathering> list = gatheringService.selectGatheringList(gathering);
        return getDataTable(list);
    }

    @GetMapping("/findAll")
    public AjaxResult findAll(){
        List<Gathering> list = gatheringService.findAll();
        return AjaxResult.success(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize(hasPermi = "gathering:gathering:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Gathering gathering) throws IOException
    {
        List<Gathering> list = gatheringService.selectGatheringList(gathering);
        ExcelUtil<Gathering> util = new ExcelUtil<Gathering>(Gathering.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize(hasPermi = "gathering:gathering:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(gatheringService.selectGatheringById(id));
    }

    @GetMapping("/findById")
    public AjaxResult findById(String id)
    {
        return AjaxResult.success(gatheringService.selectGatheringById(id));
    }

    @GetMapping("/findId/{id}")
    public AjaxResult findById2(@PathVariable("id") String id)
    {
        System.out.println("port:" + port);
        return AjaxResult.success(gatheringService.selectGatheringById(id));
    }
    /**
     * 新增活动
     */
    @PreAuthorize(hasPermi = "gathering:gathering:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Gathering gathering)
    {
        return toAjax(gatheringService.insertGathering(gathering));
    }
    @PostMapping("/add")
    public AjaxResult add2(@RequestBody Gathering gathering)
    {

        return toAjax(gatheringService.insertGathering(gathering));
    }
    /**
     * 修改活动
     */
    @PreAuthorize(hasPermi = "gathering:gathering:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Gathering gathering)
    {
        return toAjax(gatheringService.updateGathering(gathering));
    }

    /**
     * 删除活动
     */
    @PreAuthorize(hasPermi = "gathering:gathering:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(gatheringService.deleteGatheringByIds(ids));
    }

    @GetMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String header = request.getHeader("X-Request-Red");
        System.out.println("header = " + header);
        String red = request.getParameter("red");
        System.out.println("red = " + red);
        return header;
    }
}
